
import urllib.request

url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1724056969261_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'


headers = {
    # ':authority': 'dianying.taobao.com',
    # ':method': 'GET',
    # ':path': '/cityAction.json?activityId&_ksTS=1724056969261_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true',
    # ':scheme': 'https',
    'accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
    # 'accept-encoding': 'gzip, deflate, br, zstd', # 必须注释掉  不然无法解码
    'accept-language': 'zh-CN,zh;q=0.9',
    'bx-v': '2.5.14',
    'cache-control': 'no-cache',
    'cookie': 't=298e1ee71ef188674c989e89f77944de; cookie2=111a856436f162cd47d545cdfde0857c; v=0; _tb_token_=718685eb70a5b; cna=OIVJH3BmrEsCAXug4fWrqkPO; xlly_s=1; tb_city=110100; tb_cityName="sbG+qQ=="; isg=BIGB-FgFgxOKW-8mxL4KUNxCkM2brvWgNA4nf-PWkAjnyqCcK_oIcNFErD6MQo3Y',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://dianying.taobao.com/index.htm?spm=a1z21.3046609.header.3.32c0112atx7l28&n_s=new',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'x-requested-with': 'XMLHttpRequest',
}

# 请求对象定制
request = urllib.request.Request(url=url, headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)

# 获取响应的数据
content = response.read().decode('utf-8')
# print(content)

# split 切割字符串
content = content.split('(')[1].split(')')[0]
# print(content)

with open('074.解析_jsonpath解析淘票票.json', 'w', encoding='utf-8')as fp:
    fp.write(content)


# 导入json jsonpath
import json
import jsonpath

# 导入文件 (注意：文件)
obj = json.load(open('074.解析_jsonpath解析淘票票.json', 'r', encoding='utf-8'))

# jsonpath查询元素  所有城市
city_list = jsonpath.jsonpath(obj, '$..regionName')

print(city_list)
